﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YY.Admin.Core.Models;

namespace YY.Admin.Services
{
    public class UserService : IUserService
    {
        // 模拟数据，实际使用时连接数据库
        private readonly List<User> _users = new()
        {
            new User { Id = 1, Username = "admin", Email = "admin@example.com", Role = "Administrator", IsActive = true, CreatedAt = DateTime.Now.AddMonths(-6) },
            new User { Id = 2, Username = "user1", Email = "user1@example.com", Role = "User", IsActive = true, CreatedAt = DateTime.Now.AddMonths(-3) },
            new User { Id = 3, Username = "user2", Email = "user2@example.com", Role = "User", IsActive = false, CreatedAt = DateTime.Now.AddMonths(-1) }
        };

        public async Task<List<User>> GetUsersAsync()
        {
            await Task.Delay(200);
            return _users.ToList();
        }

        public async Task<User?> GetUserByIdAsync(int id)
        {
            await Task.Delay(100);
            return _users.FirstOrDefault(u => u.Id == id);
        }

        public async Task<bool> CreateUserAsync(User user)
        {
            await Task.Delay(300);
            user.Id = _users.Max(u => u.Id) + 1;
            user.CreatedAt = DateTime.Now;
            _users.Add(user);
            return true;
        }

        public async Task<bool> UpdateUserAsync(User user)
        {
            await Task.Delay(200);
            var existingUser = _users.FirstOrDefault(u => u.Id == user.Id);
            if (existingUser != null)
            {
                existingUser.Username = user.Username;
                existingUser.Email = user.Email;
                existingUser.Role = user.Role;
                existingUser.IsActive = user.IsActive;
                return true;
            }
            return false;
        }

        public async Task<bool> DeleteUserAsync(int id)
        {
            await Task.Delay(150);
            var user = _users.FirstOrDefault(u => u.Id == id);
            if (user != null)
            {
                _users.Remove(user);
                return true;
            }
            return false;
        }
    }
}
